package com.xl.studentpass.controller;

import com.xl.studentpass.entity.BaseAreaEntity;
import com.xl.studentpass.entity.SchoolInfoEntity;
import com.xl.studentpass.entity.vo.AreaVo;
import com.xl.studentpass.entity.vo.SchoolVo;
import com.xl.studentpass.service.BaseAreaService;
import com.xl.studentpass.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: xl
 * @createTime: 2023/6/5 14:39:46
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description:
 */
@RestController
@Api(tags = "地理信息")
@RequestMapping("/location")
@Slf4j
public class LocationInfoController {

    @Resource
    BaseAreaService baseAreaService;

    @PostMapping("/getProvince")
    @ApiOperation(value = "获取省份数据", response = R.class)
    public R getProvinces() {
        log.info(this.getClass().getSimpleName() + "==========================获取省份数据");
        List<BaseAreaEntity> provinces = baseAreaService.getProvince();
        if (ObjectUtils.isEmpty(provinces)) {
            return R.error();
        }
        return R.ok().put("province", provinces);
    }

    @PostMapping("/getArea")
    @ApiOperation("获取城市数据")
    public R getCity(@RequestBody AreaVo area) {
        log.info(this.getClass().getSimpleName() + "==========================获取城市数据");
        if (ObjectUtils.isEmpty(area)) {
            return R.error();
        }
        change(area.getPageNo(), area.getPageSize());
        List<BaseAreaEntity> cities = baseAreaService.getArea(area.getArea(), area.getPageNo(), area.getPageSize(), area.getProvinceId());
        if (ObjectUtils.isEmpty(cities)) {
            return R.error();
        }
        return R.ok().put("cities", cities);
    }

    @PostMapping("/getSchool")
    @ApiOperation("获取学校信息")
    public R getSchools(@RequestBody SchoolVo schoolVo) {
        log.info(this.getClass().getSimpleName() + "=============================获取学校信息");
        if (ObjectUtils.isEmpty(schoolVo)) {
            return R.error();
        }
        change(schoolVo.getPageNo(), schoolVo.getPageSize());
        List<SchoolInfoEntity> schools = baseAreaService.getSchools(schoolVo);
        return R.ok().put("schools", schools);
    }

    private void change(Integer pageNo, Integer pageSize) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
    }
}
